﻿package
{
	import flash.display.*;
	import flash.net.*;
	import flash.events.*;
	import flash.text.*;
	import flash.geom.Rectangle;
	import flash.display.MovieClip;
	 
	public class Main extends Sprite{
		
		private var imageLoader:Loader; //the asset imageLoader
		private var loadedImage:Bitmap;
		private var loadedImage1:Bitmap;
		private var loadedImage2:Bitmap;
		private var loadedImage3:Bitmap;
		private var tf:TextField = new TextField(); 
		private var progressOutput:TextField; // the text display field 
		private var txtTrace:TextField;
		private var container:Sprite = new Sprite();
		
		private var mLoader = new MLoader();

		var sr:String;
		var imgSrc:String;
		
		//Movement 
		private var markerContainer:Sprite = new Sprite() ;
		private var vx:Number;
		private var vy:Number;
		private var bounce:Number = -0.1;//-0.7;
		private var gravity:Number =.5;
		private var oldX:Number;
		private var oldY:Number;
		private var friction:Number = 0.5;
		
		private var directionChange:Boolean;

		//constructor
		public function Main(){
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align=StageAlign.TOP_LEFT;
			
			createProgressIndicator();//create the progress indicator
			
			var keyStr:String;
			var valueStr:String;
			var paramObj:Object = LoaderInfo(this.root.loaderInfo).parameters;
			//progressOutput.text="mooo";
						
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.border = true;
			addChild(tf);
			tf.x = 2000;
			
			tf.textColor = 0xffffff;
			tf.text = "start";
			trace("here");
			tf.appendText("params:" + "\n"+"^^");
			try {
				for (keyStr in paramObj) {
			        valueStr = String(paramObj[keyStr]);
			        tf.appendText("\t" + keyStr + ":\t" + valueStr + "\n");
					
					if(keyStr=="imgSrc"){
					   imgSrc=valueStr;
					}
				}
				tf.appendText("gotVar");
			} catch (error:Error) {
				    tf.appendText(error.toString());
					//imgSrc = "http://localhost/Schiebel/File.aspx?Id=650&width=0&height=0";
					//imgSrc = "http://localhost/Schiebel/Images/dbImages/Composite_Design_Capability_Strip.jpg";
			}
		
			
			//imgSrc = "http://www.armfelt.co.uk/Dev/Schiebel/File.aspx?Id=650&width=0&height=0";
			//imgSrc = "testimage.jpg";
			//var urlloaderb:URLLoader = URLLoader(event.target);
            //trace("completeHandler: " + urlloaderb.data);
			//var myXML:XML =new XML(urlloaderb.data);
			//trace(myXML.IMAGE[0].@itemURL);   
			//trace(employees.employee.name);             
			
			//create imageLoader object and register for events
			imageLoader = new Loader();//create the imageLoader
			imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,loadProgressListener);
			imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageCompleteListener);
			imageLoader.contentLoaderInfo.addEventListener(Event.INIT,imageInitListener);
			imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, imageIoErrorListener);
			if (imgSrc == null) {
				imgSrc = "http://localhost/Schiebel/Images/dbImages/Composite_Design_Capability_Strip.jpg";
				}
			imageMLoad(new URLRequest(imgSrc));//start  the load operation
			
			}
		
		private function createProgressIndicator():void{
			progressOutput = new TextField();
			progressOutput.autoSize = TextFieldAutoSize.LEFT;
			progressOutput.border=true;
			progressOutput.background=true;
			progressOutput.selectable=false;
			progressOutput.text="Loading...";
			progressOutput.x=-200;
		}
			
		private function loadProgressListener(e:ProgressEvent):void{
			progressOutput.text = "Loading: " + Math.floor(e.bytesLoaded/1024)+ "/" + Math.floor(e.bytesTotal/1024)+"kb"
		}
		
		private function imageMLoad(urlRequest:URLRequest):void{
			imageLoader.load(urlRequest);
			
			if (!contains(progressOutput)){
				addChild(mLoader);
				addChild(progressOutput);
				mLoader.y=(stage.stageHeight/2)-(mLoader.height/2);
				mLoader.x=-400;
				//mLoader.x=(stage.stageWidth/2)-(mLoader.width/2);
			}
		}
		
		private function imageInitListener(e:Event):void{
			txtTrace = new TextField();
			txtTrace.autoSize = TextFieldAutoSize.LEFT;
			txtTrace.border=true;
			txtTrace.background=true;
			txtTrace.selectable=false;
			txtTrace.x=-200;
			txtTrace.text=".";
			
			var markerShape:Shape = new Shape();
			markerShape.graphics.lineStyle(0,0x318353);
			markerShape.graphics.beginFill(0x318353,1)
			markerShape.graphics.drawRect(0,0,26,4);
			
			//markerContainer.addChild(markerShape);
			
			addChildAt(container,0);
			
			addChild(txtTrace);
			
			loadedImage = imageLoader.content as Bitmap;
			loadedImage1 = new Bitmap(loadedImage.bitmapData);
			loadedImage2 = new Bitmap(loadedImage.bitmapData);
			//loadedImage3 = new Bitmap(loadedImage.bitmapData);

						
			//loadedImage.addChildAt(imageLoader.content, 0);
			
			//loadedImage1.addChild(loadedImage);
			//loadedImage2.addChild(loadedImage);
			//container.addChild(loadedImage1);//add loaded asset to display list
			
			container.addChildAt(loadedImage1, 0);//add loaded asset to display list
			container.addChildAt(loadedImage2, 1).x = loadedImage.width;
			//container.addChildAt(loadedImage3, 2).x = (loadedImage.width)*2;
			
			container.x = 0;
			//container.x = loadedImage.width * -1;
			//container.addChildAt(loadedImage2, 1)
			//container.getChildAt(1).x = 200;
			
			
			/*var markerLine:Shape = new Shape();
			markerLine.graphics.lineStyle(0, 0x343536);
			markerLine.graphics.beginFill(0xffffff,1)
			markerLine.graphics.drawRect(0,2,980,0);
			addChild(markerLine);
			addChild(markerContainer);*/
		}
		
		//invoked when the asset has been fully loade
		private function imageCompleteListener(e:Event):void{
			tf.appendText("imageCompleteListener");
			//tf.text="imageCompleteListener";
			removeChild(progressOutput);
			removeChild(mLoader);
			nextStep();
		}
		
		//listner invoked when a load error occurs
		private function imageIoErrorListener(e:IOErrorEvent):void{
			progressOutput.text="Image Load Error";//+ e.text;
		}
		
		private function nextStep():void{
			tf.appendText("nextStep");
			//tf.text="nextStep";
			vx = -5;
			addEventListener(Event.ENTER_FRAME, mOnEnterFrame);
			container.addEventListener(MouseEvent.MOUSE_DOWN, mOnMouseDown);
			addEventListener(Event.ENTER_FRAME, mOnEnterFrame);
			
		}
		
		//////
		private function mOnEnterFrame(event:Event):void
		{
			//vx = roundDec(vx,2);
			//vx = Math.round(vx);
			//txtTrace.text = "vx::"+ vx;
			
			if (vx==0.05)
			{
				//vx = Math.round(vx);
			}
			else if (vx == 0 )
			{
				//trace("a");
				vx=(1 * 0.9)*-1; 
			}
			
			else if (vx > 3)//left
			{
				//trace("b");
				vx = vx *0.9; 
				//trace("b:"+vx);
				//trace("this:"+vx > 0 +":"+vx)
				/*else
				{
					vx = (vx * 0.9)*-1; 
				}*/
			}
			
			
			else //right
			{
				if (vx < -4)
				{
					vx = vx * 0.9; 
				}
				else
				{
					vx = vx * 1.1; 
				}
			}
//vx = Math.round(vx);
//vx=roundDec(vx,5)
			container.x += vx;
			container.y += vy;
			
			var left:Number = 0;
			var right:Number = stage.stageWidth;
			var top:Number = 0;
			var bottom:Number = stage.stageHeight;
			
			if (vx > 0)
			{
				//txtTrace.appendText(vx+" container.x:"+container.x );
				if (container.x >  (loadedImage.width - stage.stageWidth) * -1 )
				{
					//---- txtTrace.appendText("right:" + container.x +"-" + (imageLoader.content.width - stage.stageWidth) * -1 + "-" );
					//trace("container.getChildAt(0).x:" + container.getChildAt(0).x+" container.getChildAt(1).x:" + container.getChildAt(1).x);
					//container.getChildAt(1).x = -300;
					//var child0LG = container.getChildAt(0).x;
					var child0LG = container.x;
					trace("child0LG: "+child0LG);
					container.getChildAt(0).x = 0;
					container.getChildAt(1).x = loadedImage.width;
					//container.swapChildrenAt(0, 1);
					//container.x = ((loadedImage.width) * -1)-(loadedImage.width - stage.stageWidth);
					container.x = ((loadedImage.width) * -1)+child0LG;
					//container.x = child0LG + loadedImage.width;;
				}
				//trace("right")
			}
				
			if (vx < 0)
			{
				//trace("left")
				if (container.x <  (loadedImage.width) * -1)
				{
					//--- txtTrace.appendText("left-" + container.x +"-" + (imageLoader.content.width - stage.stageWidth) * -1 + "-");
					//trace("container.x:" + container.x);
					//trace("container.getChildAt(0).x:" + container.getChildAt(0).x);
					//trace("container.getChildAt(1).x:" + container.getChildAt(1).x);
					container.getChildAt(1).x = container.getChildAt(0).x - loadedImage.width;
					container.swapChildrenAt(0, 1);
					var child0LG = container.getChildAt(0).localToGlobal;
					container.getChildAt(0).x = 0;
					container.getChildAt(1).x = container.getChildAt(0).x+container.getChildAt(0).width;
					container.x = (child0LG);					
				}
			}
		}
		
		
		private function roundDec(numIn:Number, decimalPlaces:int):Number {
			var nExp:int = Math.pow(10,decimalPlaces) ;
			var nRetVal:Number = Math.round(numIn * nExp) / nExp
			return nRetVal;
		} 


		private function mOnMouseDown(event:MouseEvent):void{
						
			oldX = container.x;
			oldY = container.y;
			
			stage.addEventListener(MouseEvent.MOUSE_UP, mOnMouseUp);
//			e((imageLoader.content.width-stage.stageWidth*-1) ,0,imageLoader.content.width-stage.stageWidth,0);
			var vx:Number = (container.width - stage.stageWidth)*-1;
			var vy:Number =0 ;
			var vw:Number = container.width - stage.stageWidth;
			var vh:Number = 0;
			//tf.appendText(":"+","+vx+","+vy+","+vw+","+vh);
			//tf.text=":"+","+vx+","+vy+","+vw+","+vh;
			var bounds:Rectangle = new Rectangle(vx,vy,vw,vh);
			container.startDrag(false, bounds);
			removeEventListener(Event.ENTER_FRAME, mOnEnterFrame);
			addEventListener(Event.ENTER_FRAME, trackVelocity);
		}
		
		private function mOnMouseUp(event:MouseEvent):void{
			//tf.text="mOnMouseUp";
			stage.removeEventListener(MouseEvent.MOUSE_UP, mOnMouseUp);
			container.stopDrag();
			removeEventListener(Event.ENTER_FRAME, trackVelocity);
			addEventListener(Event.ENTER_FRAME, mOnEnterFrame);
			directionChange = true;
		}
		
		private function trackVelocity(event:Event):void{
			vx = (container.x - oldX);
			
			//vy = (container.y - oldY);
			oldX = container.x;
			//oldY = container.y;
		}
		//////
	}
}
